package linter.controller.blog;

import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import linter.POJO.DTO.CollectDTO;
import linter.POJO.DTO.UserDTO;
import linter.custom.ResultVO;
import linter.service.CollectService;
import linter.service.LikesService;
import linter.service.PostsService;
import linter.service.UserService;
import linter.utils.JwtUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

import static linter.custom.CustomResultType.*;

/**
 * @author 北煜
 */
@Api(tags = "论坛状态控制器")
@RestController
@Slf4j
@RequestMapping("/BlogStatus")
public class BlogStatusController {

    @Resource
    UserService userService;

    @Resource
    PostsService postsService;

    @Resource
    LikesService likesService;

    @Resource
    CollectService collectService;

    @GetMapping("/show/getUserByUsername/{username}")
    @ApiOperation("根据用户名查找用户")
    public ResultVO<UserDTO> getUserByUsername(@PathVariable String username) {
        UserDTO userDTO = userService.getUserByUsername(username);
        return ResultVO.success(userDTO);
    }

    @PostMapping("/likes")
    @ApiOperation("点赞")
    public ResultVO<String> likes(Integer postsId){
        String username = JwtUtils.getUserName((String) SecurityUtils.getSubject().getPrincipal());
        Long uuid = userService.getIdByUsername(username);
        if (uuid != null){
            if (postsService.selPosts(postsId) != null){
                if (postsService.updateLikes(postsId, uuid)){ return ResultVO.success(likesService.updateUuid(uuid,postsId)); }
                return ResultVO.error("已经点赞过了");
            }
            return ResultVO.error(POSTS_NOT_EXIST);
        }
        return ResultVO.error(USER_NOT_EXIST);
    }

    @PostMapping("/likesUpdate")
    @ApiOperation("取消点赞")
    public ResultVO<String> likesUpdate(Integer postsId){
        String username = JwtUtils.getUserName((String) SecurityUtils.getSubject().getPrincipal());
        Long uuid = userService.getIdByUsername(username);
        if (uuid != null){
            if (postsService.selPosts(postsId) != null){
                System.out.println(postsService.updateLike(postsId));
                if (postsService.updateLike(postsId)){ return ResultVO.success(likesService.updateLikeByUuid(uuid,postsId)); }
                return ResultVO.error("取消点赞失败");
            }
            return ResultVO.error(POSTS_NOT_EXIST);
        }
        return ResultVO.error(USER_NOT_EXIST);
    }

    // TODO：前端需要验证用户在一段时间之内连续查看是不能触发浏览量的增加
    @PostMapping("/views")
    @ApiOperation("增加浏览量")
    public ResultVO<String> views(Integer postsId){
        if (postsService.selPosts(postsId) != null){
            if (postsService.updateViews(postsId)){ return ResultVO.success(VIEWS_SUCCESS); }
            return ResultVO.error(VIEWS_ERROR);
        }
        return ResultVO.error(USER_NOT_EXIST);
    }

    @PostMapping("/createCollect")
    @ApiOperation("创建收藏夹")
    public ResultVO<String> createCollect(String collectName, @RequestParam(required = false) Integer collectFatherId){
        String username = JwtUtils.getUserName((String) SecurityUtils.getSubject().getPrincipal());
        Long uuid = userService.getIdByUsername(username);
        // TODO: 可以将两个并在一起 后续优化
        if (uuid != null){
            if (collectFatherId == null){ return ResultVO.success(collectService.create(collectName, uuid)); }
            return ResultVO.success(collectService.createByFather(collectName, uuid, collectFatherId));
        }
        return ResultVO.error(USER_NOT_EXIST);
    }

    @PostMapping("/selAllCollectByUuid")
    @ApiOperation("查询用户所有收藏夹")
    public ResultVO<PageInfo<CollectDTO>> selAllCollectByUuid(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                                              @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize){
        String username = JwtUtils.getUserName((String) SecurityUtils.getSubject().getPrincipal());
        Long uuid = userService.getIdByUsername(username);
        if (uuid != null){ return ResultVO.success(collectService.selAllByUuid(pageNum,pageSize,uuid)); }
        return ResultVO.error(USER_NOT_EXIST);
    }

    @PostMapping("/delCollect")
    @ApiOperation("删除收藏夹")
    public ResultVO<String> delCollect(Integer collectId){
        String username = JwtUtils.getUserName((String) SecurityUtils.getSubject().getPrincipal());
        Long uuid = userService.getIdByUsername(username);
        if (uuid != null){ return ResultVO.success(collectService.del(collectId)); }
        return ResultVO.error(USER_NOT_EXIST);
    }

    @PostMapping("/updateCollect")
    @ApiOperation("修改收藏夹")
    public ResultVO<?> updateCollect(Integer collectId, String collectName) {
        String username = JwtUtils.getUserName((String) SecurityUtils.getSubject().getPrincipal());
        Long uuid = userService.getIdByUsername(username);
        if (uuid != null){ return ResultVO.success(collectService.updateCollect(collectId, collectName, uuid)); }
        return ResultVO.error(USER_NOT_EXIST);
    }

    @PostMapping("/moveCollect")
    @ApiOperation("移动收藏夹")
    public ResultVO<String> moveCollect(Integer collectId, Integer collectFatherId){
        String username = JwtUtils.getUserName((String) SecurityUtils.getSubject().getPrincipal());
        Long uuid = userService.getIdByUsername(username);
        if (uuid != null){ return ResultVO.success(collectService.moveCollect(collectId, collectFatherId, uuid)); }
        return ResultVO.error(USER_NOT_EXIST);
    }

    @PostMapping("/collectPosts")
    @ApiOperation("收藏文章")
    public ResultVO<String> collectPosts(Integer postsId, Long collectId){
        String username = JwtUtils.getUserName((String) SecurityUtils.getSubject().getPrincipal());
        Long uuid = userService.getIdByUsername(username);
        if (uuid != null){ return ResultVO.success(collectService.collectPosts(uuid,postsId,collectId)); }
        return ResultVO.error(USER_NOT_EXIST);
    }

    @PostMapping("/delCollectPosts")
    @ApiOperation("收藏夹删除文章")
    public ResultVO<?> delCollectPosts(Integer collectId,Integer postsId){
        String username = JwtUtils.getUserName((String) SecurityUtils.getSubject().getPrincipal());
        Long uuid = userService.getIdByUsername(username);
        if (uuid != null){ return ResultVO.success(collectService.delCollectPosts(uuid,collectId,postsId)); }
        return ResultVO.error(USER_NOT_EXIST);
    }

    @PostMapping("/selCollectPosts")
    @ApiOperation("查询收藏夹文章")
    public ResultVO<PageInfo<?>> selCollectPosts(Integer collectId){
        String username = JwtUtils.getUserName((String) SecurityUtils.getSubject().getPrincipal());
        Long uuid = userService.getIdByUsername(username);
        if (uuid != null){ return ResultVO.success(collectService.selCollectPosts(uuid,collectId)); }
        return ResultVO.error(USER_NOT_EXIST);
    }

    @PostMapping("/selCollectNote")
    @ApiOperation("查询收藏夹笔记")
    public ResultVO<PageInfo<?>> selCollectNote(Integer collectId){
        String username = JwtUtils.getUserName((String) SecurityUtils.getSubject().getPrincipal());
        Long uuid = userService.getIdByUsername(username);
        if (uuid != null){ return ResultVO.success(collectService.selCollectNote(uuid,collectId)); }
        return ResultVO.error(USER_NOT_EXIST);
    }

}
